package com.gitee.zycra.future.job.common.container;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zycra
 */
@Component
public class WebSocketSessionContainer {

    private static final String SUB_LINE = "_";

    private final Map<String, WebSocketSession> sessionMap = new ConcurrentHashMap<>();

    public WebSocketSession getSession(String applicationName, String ip) {
        return sessionMap.get(getKey(applicationName, ip));
    }

    public void setSession(String applicationName, String ip, WebSocketSession webSocketSession) {
        sessionMap.put(getKey(applicationName, ip), webSocketSession);
    }

    public void removeSession(String applicationName, String ip) {
        sessionMap.remove(getKey(applicationName, ip));
    }

    private String getKey(String applicationName, String ip) {
        return applicationName + SUB_LINE + ip;
    }

}
